﻿#region

using System.Linq;
using xConnected.Core.Common.Paging;
using xConnected.Core.Interfaces.Paging;
using xConnected.Core.Model;

#endregion

namespace xConnected.Data
{
    public partial class LanguageRepository
    {
        

        #region ILanguageRepository Members

        public override void Delete(Language entity)
        {
            DbSet.Remove(DbSet.Single(c => c.Id == entity.Id));
        }

        #endregion

        //PAGING
        public IPage<Language> GetAllPaged(int pageIndex = 0, int pageSize = 10)
        {
            var data = DbSet.OrderBy(k => k.Id).Skip(pageSize*pageIndex).Take(pageSize).AsEnumerable();
            return new Page<Language>(data, DbSet.Count(), pageSize, pageIndex);
        }

        //PAGING+SEARCH
        public IPage<Language> GetAllPagedSearched(string search = "", int pageIndex = 0, int pageSize = 10)
        {
            var searchTerm = search.ToLower();
            var searchedLanguages = DbSet.Where(l =>
                                                l.ShortName.ToLower().Contains(searchTerm) ||
                                                l.Description.ToLower().Contains(searchTerm));
            var data = searchedLanguages.OrderBy(k => k.Id).Skip(pageSize*pageIndex).Take(pageSize).AsEnumerable();

            return new Page<Language>(data, searchedLanguages.Count(), pageSize, pageIndex);
        }
    }
}